package com.news.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.news.bean.NewsType;
import com.news.dao.NewsTypeDao;
import com.news.util.DButil;

public class NewsTypeDaoImpl implements NewsTypeDao {

    private final String ADD = "insert into newstype values(null,?,?)";
    private final String EDIT = "update newstype set name=?,sort=? where id=?";
    private final String DELETE = "delete from newstype where id= ?";
    private final String LIST = "select * from newstype order by sort";
    private final String GET = "select * from newstype where id=?";
    private final String TOTAL = "select count(*) from newstype";

    public List<NewsType> list(Map<Object, Object> map) {
        // TODO Auto-generated method stub
        List<NewsType> list = new ArrayList<NewsType>();
        PreparedStatement pstmt = DButil.getPreparedStatement(LIST);
        try {
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                NewsType newsType = new NewsType();
                newsType.setName(rs.getString("name"));
                newsType.setSort(rs.getInt("sort"));
                newsType.setId(rs.getInt("id"));
                list.add(newsType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close();
        }
        return list;
    }
//	public List<NewsType> getNewsType(Map<?, ?> map) {
//	}

    public List<NewsType> list() {
        // TODO Auto-generated method stub
        List<NewsType> list = new ArrayList<NewsType>();
        PreparedStatement pstmt = DButil.getPreparedStatement(LIST);
        try {
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                NewsType newsType = new NewsType();
                newsType.setName(rs.getString("name"));
                newsType.setSort(rs.getInt("sort"));
                newsType.setId(rs.getInt("id"));
                list.add(newsType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close();
        }
        return list;
    }

    public int add(NewsType nt) {
        int ret = 0, i = 1;
        try {
            PreparedStatement pstmt = DButil.getPreparedStatement(ADD);

            pstmt.setString(i++, nt.getName());
            pstmt.setInt(i++, nt.getSort());
            ret = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close();
        }
        return ret;
    }

    public int edit(NewsType nt) {
        System.out.println("--------------new edit---------------------");
        System.out.println("--------------new edit--" + nt.getName() + "-------------------");
        System.out.println("--------------new edit-----" + nt.getSort() + "----------------");
        System.out.println("--------------new edit------" + nt.getId() + "---------------");

        int ret = 0, i = 1;
        try {
            PreparedStatement pstmt = DButil.getPreparedStatement(EDIT);

            pstmt.setString(i++, nt.getName());
            pstmt.setInt(i++, nt.getSort());
            pstmt.setInt(i++, nt.getId());
            ret = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close();
        }
        return ret;
    }

    public int del(Integer id) {
        int ret = 0;
        try {
            PreparedStatement pstmt = DButil.getPreparedStatement(DELETE);
            pstmt.setInt(1, id);
            ret = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
//            return 0;
        } finally {
            DButil.close();
        }
        return ret;
    }

    public int get(Integer id) {
        NewsType newsType = new NewsType();
        try {
            PreparedStatement pstmt = DButil.getPreparedStatement(GET);
            pstmt.setInt(1, id);
            System.out.println("id id id==============" + id);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                newsType.setName(rs.getString("name"));
                newsType.setSort(rs.getInt("sort"));
                newsType.setId(rs.getInt("id"));
                System.out.println("rs.name" + rs.getString("name"));
                System.out.println("rs.sort" + rs.getInt("sort"));
                System.out.println("rs.id" + rs.getInt("id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close();
        }
        return 0;
    }

    public NewsType getR(Integer id) {
        NewsType newsType = new NewsType();
        try {
            PreparedStatement pstmt = DButil.getPreparedStatement(GET);
            pstmt.setInt(1, id);
            System.out.println("id id id==============" + id);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                newsType.setName(rs.getString("name"));
                newsType.setSort(rs.getInt("sort"));
                newsType.setId(rs.getInt("id"));
                System.out.println("rs.name" + rs.getString("name"));
                System.out.println("rs.sort" + rs.getInt("sort"));
                System.out.println("rs.id" + rs.getInt("id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close();
        }
        return newsType;
    }

    public int Total() {
        int ret = 0;
        PreparedStatement pstmt = DButil.getPreparedStatement(TOTAL);
        try {
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            ret = rs.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close();
        }
        return ret;
    }
}
